#include "RAT_EntityManager.h"

namespace RAT_ENGINE
{
	RAT_EntityManager::RAT_EntityManager()
	{
	}

	RAT_EntityManager::~RAT_EntityManager()
	{
	}

	void RAT_EntityManager::Init(RAT_ResourceManager* argResourceManager)
	{
		resourceManager = argResourceManager;
	}

	void RAT_EntityManager::Init(RAT_ResourceManager* argResourceManager, std::string argEntitiesFileURL)
	{
		resourceManager = argResourceManager;

		std::ifstream entitiesFile;
		entitiesFile.open(argEntitiesFileURL);

		while (entitiesFile.good())
		{
			std::string currentLine;
			std::string type;
			std::string meshURL;

			std::getline(entitiesFile, currentLine);

			type = currentLine.substr(0, currentLine.find(";"));
			meshURL = currentLine.substr(currentLine.find(";") + 1);
			
			if (type == "entitycustommodel")
			{
				RAT_EntityCustomModel* ecm = new RAT_EntityCustomModel();
				ecm->Init(entityCustomModels.size(), resourceManager->GetCustomMesh(meshURL)); 

				AddEntityCustomModel(ecm);
			}
		}

		entitiesFile.close();
	}

	void RAT_EntityManager::CleanUp()
	{
	}
	
	RAT_ResourceManager* RAT_EntityManager::GetResourceManager() const
	{
		return resourceManager;
	}

	//RAT_Entity* RAT_EntityManager::GetEntity(int argID) const
	//{
	//	return entities[argID];
	//}
	//
	//std::vector<RAT_Entity*> RAT_EntityManager::GetEntities() const
	//{
	//	return entities;
	//}

	RAT_EntityCustomModel* RAT_EntityManager::GetEntityCustomModel(int argID) const
	{
		return entityCustomModels[argID];
	}

	RAT_EntityCustomModel* RAT_EntityManager::GetEntityCustomModel(std::string argURL) const
	{
		unsigned int index = 0;
		while (index < entityCustomModels.size() && entityCustomModels[index]->GetMesh()->GetURL() != argURL)
		{
			++index;
		}
			
		return entityCustomModels[index];
	}

	//void RAT_EntityManager::AddEntity(RAT_Entity* argEntity)
	//{
	//	entities.push_back(argEntity);
	//}

	void RAT_EntityManager::AddEntityCustomModel(RAT_EntityCustomModel* argECM)
	{
		entityCustomModels.push_back(argECM);
	}

	void RAT_EntityManager::PrintIO() const
	{
		/*std::cout << "Entities: " << std::endl;
		for (unsigned int i = 0; i < entities.size(); ++i)
		{
			entities[i]->PrintIO();
			std::cout << std::endl;
		}*/
		std::cout << "EntityCustomModels: " << std::endl;
		for (unsigned int i = 0; i < entityCustomModels.size(); ++i)
		{
			entityCustomModels[i]->PrintIO();
			std::cout << std::endl;
		}
	}
}